<template>
	<view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="button-sp-area">
				<button type="default" @click="uploadImage()">图片上传</button>
				<!-- #ifdef H5 -->
					<button type="default" @click="upload()">文件上传</button>
				<!-- #endif -->
			</view>
		</view>
		<view v-if="imgurl!=''">
			<image :src="imgurl"></image>
		</view>
	</view>
</template>

<script>
	import fileservice from '../../myservice/fileservice.js'
	
	export default{
		data(){
			return {
				imgurl:''
			}
		},
		methods:{
			uploadImage:function(){
				var that=this;
				uni.chooseImage({
					count:1,
					success:function(res){
						console.log(res.tempFilePaths[0])
						fileservice.uploadImage(res.tempFilePaths[0]).then(re=>{
							console.log(JSON.stringify(re))
							if(re.statusCode==200){
								var data=JSON.parse(re.data);
								var fileId=data.Id;
								fileservice.GetFile(fileId).then(r=>{
									console.log(JSON.stringify(r))
									if(r.statusCode==200){
									that.imgurl=r.tempFilePath
									}else{
										uni.showToast({
											title: r.errMsg,
											icon:'none'
										});
									}
								})
							}else{
								uni.showToast({
									title: '上传失败'
								});
							}
						})
					},
					fail:function(ex){
						uni.showToast({
							title: '选择图片失败'
						});
					}
				})
			},
			upload:function(){
				uni.chooseFile({
					count:1,
					type:'all',
					success:function(res){
						console.log(res.tempFilePaths[0])
						fileservice.uploadImage(res.tempFilePaths[0]).then(res=>{
							console.log(JSON.stringify(res))
							
						})
					},
					fail:function(ex){
						uni.showToast({
							title: '选择图片失败'
						});
					}
				})
			}
		}
	}
</script>

<style>
</style>
